package com.javagroup.chessonline.view;

import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JPanel;

import com.javagroup.chessonline.callback.ChooseRoomCallback;
import com.javagroup.chessonline.callback.CreateRoomCallback;
import com.javagroup.chessonline.callback.JoinLeagueCallback;
import com.javagroup.chessonline.callback.LeagueCallback;
import com.javagroup.chessonline.callback.LobbyBackBtnCallback;
import com.javagroup.chessonline.callback.UpdateRoomCallback;
import com.javagroup.chessonline.client.Client;
import com.javagroup.chessonline.pojo.ChessOnlineRoom;
import com.javagroup.chessonline.widget.MyButtonWidget;
import com.javagroup.chessonline.widget.UserInfoWidget;
import com.thuan.hibernate.Repository;
import com.thuan.hibernate.Utils;
import com.thuan.pojo.League;
import com.thuan.pojo.UserAccount;

public class LobbyView extends ChessOnlineView  implements MouseListener{

	private UserInfoWidget mUserInfoWidget;
	private List<MyButtonWidget> mRooms = new ArrayList<>();
	private List<MyButtonWidget> mLeagues = new ArrayList<>();
	private int mCurrentPageIndex = 0;
	//private JPanel mLobbyContainer;
	private MyButtonWidget mNextPageBtn;
	private MyButtonWidget mPrevPageBtn;
	private MyButtonWidget mBackBtn;
	private MyButtonWidget mCreateRoomBtn;
	private MyButtonWidget mLeagueBtn;
	
	public LobbyView(Client mainScreen) {
		super(mainScreen);
		
		mUserInfoWidget = new UserInfoWidget(Repository.findUser(Utils.getCurrentUser()), 20, 20);
		
		JPanel userPanel = new JPanel();
		userPanel.setSize(200, 600);
		userPanel.add(mUserInfoWidget);
		
		//mLobbyContainer = new JPanel();
		//mLobbyContainer.setSize(600, 600);
		
		mNextPageBtn = new MyButtonWidget(490, 475, "Resources/Images/Shift_Icon_Right.png",
				"Resources/Images/Shift_Icon_Right.png", "", new UpdateRoomCallback(this, 1));
		mPrevPageBtn = new MyButtonWidget(390, 475, "Resources/Images/Shift_Icon_Left.png", 
				"Resources/Images/Shift_Icon_Left.png", "", new UpdateRoomCallback(this, -1));
		mBackBtn = new MyButtonWidget(650, 450, "Resources/Images/back-icon.png", 
				"Resources/Images/back-icon.png", "", new LobbyBackBtnCallback(mMainScreen, this));
		mCreateRoomBtn = new MyButtonWidget(10, 200, "Resources/Images/create-btn.png",
				"Resources/Images/create-btn.png", "", new CreateRoomCallback(mMainScreen));
		mLeagueBtn = new MyButtonWidget(0, 340, "Resources/Images/league-icon.png", 
				"Resources/Images/league-icon.png", "", new LeagueCallback(this));
		
		this.add(userPanel, BorderLayout.WEST);
		//this.add(mLobbyContainer, BorderLayout.EAST);
		this.add(mNextPageBtn);
		this.add(mPrevPageBtn);
		this.add(mBackBtn);
		this.add(mCreateRoomBtn);
		this.add(mLeagueBtn);
		this.addMouseListener(this);
	}
	
	private void addRoom() {
		synchronized (this) {
			// TODO: Hard code the room info
			List<ChessOnlineRoom> rooms = new ArrayList<ChessOnlineRoom>();
			UserAccount playerA = new UserAccount("theturtle232", "123", "Riki", 100, 10, 1, null);
			UserAccount playerB = new UserAccount("B1", "123", "Boundy", 200, 3, 5, null);
			rooms.add(new ChessOnlineRoom(1, "Name", playerA, playerB, 10, false));
			rooms.add(new ChessOnlineRoom(2, "Name", playerA, null, 20, false));
			rooms.add(new ChessOnlineRoom(3, "Name", playerA, playerB, 30, false));
			rooms.add(new ChessOnlineRoom(1, "Name", playerA, null, 10, false));
			rooms.add(new ChessOnlineRoom(2, "Name", playerA, null, 20, false));
			rooms.add(new ChessOnlineRoom(3, "Name", playerA, playerB, 30, false));
			rooms.add(new ChessOnlineRoom(1, "Name", playerA, null, 10, false));
			rooms.add(new ChessOnlineRoom(2, "Name", playerA, playerB, 20, false));
			rooms.add(new ChessOnlineRoom(2, "Name", playerA, null, 20, false));
			rooms.add(new ChessOnlineRoom(2, "Name", playerA, null, 20, false));
			
			//mLobbyContainer.removeAll();
			mRooms.clear();
			mLeagues.clear();
			
			int initX;
			int initY = 20;
			int deltaX = 150;
			int deltaY = 150;
			int maxRoomPerRow = 3;
			int maxRoomPerCol = 4;
			String roomBg = null;
			String fullRoomBg = null;
			
			roomBg = "Resources/Images/white-piece.png";
			fullRoomBg = "Resources/Images/black-piece.png";
			
			for(int i = 0; i < maxRoomPerRow && i * maxRoomPerRow < rooms.size(); i++) {
				initX  = 190;
				for(int j = 0; j < maxRoomPerCol; j++) {
					ChessOnlineRoom room =  rooms.get(i * maxRoomPerRow + j);
					String bg = room.getPlayerB() == null ? roomBg : fullRoomBg;
					MyButtonWidget cmd = new MyButtonWidget(initX , initY, bg, bg, 
							String.valueOf(room.getRoomId()), new ChooseRoomCallback(mMainScreen, room));
					if(room.getPlayerB() != null) {
						cmd.disable();
					}
					
					this.mRooms.add(cmd);
					
					if(i * maxRoomPerRow + j == rooms.size() - 1)
						break;
					
					initX += deltaX;
				}
				
				initY += deltaY;
			}
			
			repaintComponent();
		}
	}
	
	private void repaintComponent() {
		this.removeAll();
		
		this.add(mUserInfoWidget);
		this.add(mNextPageBtn);
		this.add(mPrevPageBtn);
		this.add(mBackBtn);
		this.add(mCreateRoomBtn);
		this.add(mLeagueBtn);
		
		for(MyButtonWidget room : mRooms) {
			this.add(room);
		}
		
		for(MyButtonWidget league : mLeagues) {
			this.add(league);
		}
		
		revalidate();
		repaint();
	}

	public void showRoom() {
		
		if(mCurrentPageIndex < 0)
			return;
		
		addRoom();
	}
	
	public void showLeague() {
		synchronized (this) {
			// Enter the League Mode
			Utils.enterLeagueMode();
			
			mRooms.clear();
			mLeagues.clear();
			
			// TODO: Get all league from server and then init the league icon with the info
			
			// Hard code for now
			List<League> leaguesList = new ArrayList<League>();
			leaguesList.add(new League("Warcraft League", null, null, null));
			leaguesList.add(new League("Champain League", null, null, null));
			leaguesList.add(new League("Europa League", null, null, null));
			leaguesList.add(new League("Dota League", null, null, null));
			
			int initX;
			int initY = 20;
			int deltaX = 150;
			int deltaY = 150;
			int maxLeaguePerRow = 3;
			int maxLeaguePerCol = 2;
			String leagueBg = "Resources/Images/league-icon.png";
			
			for(int i = 0; i < maxLeaguePerRow && i * maxLeaguePerRow < leaguesList.size(); i++) {
				initX  = 190;
				for(int j = 0; j < maxLeaguePerCol; j++) {
					League league = leaguesList.get(i * maxLeaguePerRow + j);
					MyButtonWidget cmd = new MyButtonWidget(initX , initY, leagueBg, leagueBg, 
							league.getName(), new JoinLeagueCallback(this, league.getLeagueId()));
					
					this.mLeagues.add(cmd);
					
					if(i * maxLeaguePerRow + j == leaguesList.size() - 1)
						break;
					
					initX += deltaX;
				}
				
				initY += deltaY;
			}
			
			repaintComponent();
		}
	}
	
	public void showLeagueRooms(int leagueId) {
		synchronized (this) {
			// Save the League Id
			Utils.saveLeagueId(leagueId);
			
			
			mRooms.clear();
			mLeagues.clear();
			
			// TODO: Get the League Room from server with specific ID
			List<ChessOnlineRoom> rooms = new ArrayList<ChessOnlineRoom>();
			UserAccount playerA = new UserAccount("theturtle232", "123", "Riki", 100, 10, 1, null);
			UserAccount playerB = new UserAccount("B1", "123", "Boundy", 200, 3, 5, null);
			rooms.add(new ChessOnlineRoom(1, "Name", playerA, playerB, 10, true));
			rooms.add(new ChessOnlineRoom(2, "Name", playerA, null, 20, true));
			rooms.add(new ChessOnlineRoom(3, "Name", playerA, playerB, 30, true));
			rooms.add(new ChessOnlineRoom(1, "Name", playerA, null, 10, true));
			rooms.add(new ChessOnlineRoom(2, "Name", playerA, null, 20, true));
			
			int initX;
			int initY = 20;
			int deltaX = 150;
			int deltaY = 150;
			int maxRoomPerRow = 3;
			int maxRoomPerCol = 4;
			String roomBg = null;
			String fullRoomBg = null;
			
			roomBg = "Resources/Images/white-piece.png";
			fullRoomBg = "Resources/Images/black-piece.png";
			
			for(int i = 0; i < maxRoomPerRow && i * maxRoomPerRow < rooms.size(); i++) {
				initX  = 190;
				for(int j = 0; j < maxRoomPerCol; j++) {
					ChessOnlineRoom room =  rooms.get(i * maxRoomPerRow + j);
					String bg = room.getPlayerB() == null ? roomBg : fullRoomBg;
					MyButtonWidget cmd = new MyButtonWidget(initX , initY, bg, bg, 
							String.valueOf(room.getRoomId()), new ChooseRoomCallback(mMainScreen, room));
					if(room.getPlayerB() != null) {
						cmd.disable();
					}
					
					this.mRooms.add(cmd);
					
					if(i * maxRoomPerRow + j == rooms.size() - 1)
						break;
					
					initX += deltaX;
				}
				
				initY += deltaY;
			}
			
			repaintComponent();
		}
	}

	@Override
	protected void paintComponent(Graphics g) {
		super.paintComponent(g);
		mUserInfoWidget.paint(g);
		mNextPageBtn.paint(g);
		mPrevPageBtn.paint(g);
		mBackBtn.paint(g);
		mCreateRoomBtn.paint(g);
		mLeagueBtn.paint(g);
		
		for(MyButtonWidget room : mRooms) {
			room.paint(g);
		}
		
		for(MyButtonWidget league : mLeagues) {
			league.paint(g);
		}
	}

	@Override
	public void mouseClicked(MouseEvent e) {
		synchronized (this) {
			
			for(MyButtonWidget room : mRooms) {
				room.onClick(e);
			}
			
			// TODO: When draw the League room, this will clear the current mLeagues list,
			// so we need the synchronized to control the concurrent issue. But for now,
			// I don't know why I cannot use the synchronized function for do it
			// Please help me find the solution!
			for(MyButtonWidget league : mLeagues) {
				league.onClick(e);
			}
		}
		
		mNextPageBtn.onClick(e);
		mPrevPageBtn.onClick(e);
		mBackBtn.onClick(e);
		mCreateRoomBtn.onClick(e);
		mLeagueBtn.onClick(e);
	}

	@Override
	public void mouseEntered(MouseEvent e) {
		
	}

	@Override
	public void mouseExited(MouseEvent e) {
		
	}

	@Override
	public void mousePressed(MouseEvent e) {
		
	}

	@Override
	public void mouseReleased(MouseEvent e) {
		
	}

	public int getCurrentPageIndex() {
		return mCurrentPageIndex;
	}

	public void setCurrentPageIndex(int currentPageIndex) {
		this.mCurrentPageIndex = currentPageIndex;
	}

	
	

}
